package com.zhangkunhao.notes.controller;

import com.zhangkunhao.notes.entity.DO.Activity;
import com.zhangkunhao.notes.entity.DO.Jokes;
import com.zhangkunhao.notes.entity.DO.User;
import com.zhangkunhao.notes.service.ActivityService;
import com.zhangkunhao.notes.service.JokeService;
import com.zhangkunhao.notes.service.UserService;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 管理员菜单
 *
 * @author 张坤浩
 */
public class AdministratorMenuListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        UserService userService = new UserService();
        Scanner sc = new Scanner(System.in);
        ActivityService activityService = new ActivityService();
        JokeService jokeService = new JokeService();
        //判断是哪个按钮
        switch (e.getActionCommand()) {
            case "查询所有用户信息":
                List<User> users = userService.queryList(0);
                if (users != null) {
                    System.out.println("--------------------所有用户信息--------------------");
                    users.forEach(System.out::println);
                }
                break;
            case "发布各种积攒功德的活动":
                System.out.println("请输入活动的名称,限制报名人数，活动的赠送功德量,活动截止日期：日期格式如2020-01-03");
                System.out.println("请输入活动的名称:");
                String activityName = sc.next();
                System.out.println("请输入限制报名人数:");
                String limitNumber = sc.next();
                System.out.println("请输入活动的赠送功德量:");
                String gainVirtue = sc.next();
                System.out.println("请输入活动截止日期: 日期格式如,2020-01-03");
                String deadline = sc.next();
                //调用service层
                String result = activityService.publishActivity(activityName, limitNumber, gainVirtue, deadline);
                System.out.println(result);
                break;
            case "查询所有活动信息":
                //调用service层
                List<Activity> activities = activityService.readAllActivities(0);
                System.out.println("---------------------所有活动信息---------------------");
                activities.forEach(System.out::println);
                break;
            case "搜索指定用户":
                System.out.println("请输入指定用户的用户名，至少包含一个关键字");
                String fuzzyName = sc.next();
                //模糊查询
                List<User> fuzzyQuery = userService.fuzzyQuery(fuzzyName);
                //这里注意sql语句如何拼接的问题
                if (fuzzyQuery.size() == 0) {
                    System.out.println("找不到符合该搜索条件的用户");
                } else {
                    System.out.println("符合该搜索条件的用户:");
                    fuzzyQuery.forEach(System.out::println);
                }
                break;
            case "批量发布功德活动":
                System.out.println("请输入活动的主体名称,统一截止日期：日期格式如2020-01-03，限制报名人数的取值范围，活动赠送功德量的取值范围");
                System.out.println("请输入活动的主体名称:");
                String activityNames = sc.next();
                System.out.println("请输入限制报名人数的取值范围，任一活动最大报名人数数量为：");
                String maxLimitNumber = sc.next();
                System.out.println("请输入活动赠送功德量的取值范围，任一活动最大赠送功德量为：");
                String maxGainVirtue = sc.next();
                System.out.println("请输入活动统一截止日期:");
                String ddl = sc.next();
                System.out.println("请输入你想要批量发布的活动数量：");
                String batchActivitiesNumber = sc.next();
                //从service层返回结果
                String result1 = activityService.batchPublishActivities(activityNames, maxLimitNumber, maxGainVirtue, ddl, batchActivitiesNumber);
                //查看批量发布是否成功
                System.out.println(result1);
                break;
            case "查看某个⽤户的功德消耗、获取情况":
                System.out.println("请输入你想查看的用户功德情况的用户名：");
                String userName = sc.next();
                //调用service层，得到查看的用户看过的笑话集合
                ArrayList<Jokes> hisReadingJokes = jokeService.jokesDetails(userName, userService);
                if (hisReadingJokes.size() > 0) {
                    System.out.println("该用户看笑话消耗功德的情况:");
                    for (Jokes joke : hisReadingJokes) {
                        System.out.println("笑话标题：" + joke.getTitle() + " 消耗功德："
                                + joke.getConsumingVirtue() + " 发布时间：" + joke.getLaunchTime());
                    }
                } else {
                    System.out.println("无浏览地狱笑话的记录");
                }
                //调用service层，得到查看的用户参加的活动集合
                List<Activity> hisParticipatingActivities = activityService.activitiesDetails(userName);
                if (hisParticipatingActivities.size() > 0) {
                    System.out.println("该用户参加活动获得功德的情况：");
                    for (Activity activity : hisParticipatingActivities) {
                        System.out.println("活动标题：" + activity.getActivityName() + " 获得功德："
                                + activity.getGainVirtue() + " 报名截止时间：" + activity.getDeadline());
                    }
                } else {
                    System.out.println("无参加活动的记录");
                }
                //调用service层，得到查看的用户的功德情况
                User user = userService.queryByUserName(userName);
                if (user != null) {
                    System.out.println("所查看用户的用户名：" + user.getUserName() + " 总功德：" + user.getInitVirtue()
                            + " 消耗功德：" + user.getPayVirtue() + " 获得功德：" + user.getGainVirtue());
                }
                break;
            default:
                System.out.println("代码是不会执行到这个位置的啦");
                break;
        }
    }
}
